//
//  EWDishInfoCell.m
//  haveSomethingToEat
//
//  Created by 陈 宁权 on 2017/7/11.
//  Copyright © 2017年 陈 宁权. All rights reserved.
//

#import "EWDishInfoCell.h"

@interface EWDishInfoCell()

@property (nonatomic,strong) UIImageView *logoView;
@property (nonatomic,strong) UILabel *dishName;
@property (nonatomic,strong) UILabel *dishDescription;
@property (nonatomic,strong) UILabel *monthOrders;
@property (nonatomic,strong) UILabel *price;
@property (nonatomic,strong) UIButton *removeShopCart;
@property (nonatomic,strong) UILabel *dishCount;

@end

@implementation EWDishInfoCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"EWDishInfoCell";
    EWDishInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[EWDishInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupChildViews];
    }
    return self;
}

- (void)setupChildViews
{
    _logoView = [[UIImageView alloc] init];
    [self addSubview:_logoView];
    
    _dishName = [[UILabel alloc] init];
    _dishName.textColor = [UIColor blackColor];
    [self addSubview:_dishName];
    
    _dishDescription = [[UILabel alloc] init];
    _dishDescription.textColor = [UIColor darkGrayColor];
    _dishDescription.font = [UIFont systemFontOfSize:13];
    _dishDescription.numberOfLines = 0;
    _dishDescription.lineBreakMode = NSLineBreakByCharWrapping;
    [self addSubview:_dishDescription];
    
    _monthOrders = [[UILabel alloc] init];
    _monthOrders.font = [UIFont systemFontOfSize:12];
    _monthOrders.textColor = [UIColor darkGrayColor];
    [self addSubview:_monthOrders];
    
    _price = [[UILabel alloc] init];
    [self addSubview:_price];
    
    _removeShopCart = [UIButton buttonWithType:UIButtonTypeCustom];
    [_removeShopCart setBackgroundImage:[UIImage imageNamed:@"removeFromShopCert"] forState:UIControlStateNormal];
    [_removeShopCart addTarget:self action:@selector(removeFromShopCart) forControlEvents:UIControlEventTouchUpInside];
    _removeShopCart.layer.cornerRadius = 10.0f;
    _removeShopCart.layer.masksToBounds = YES;
    _removeShopCart.hidden = YES;
    [self addSubview:_removeShopCart];
    
    _dishCount = [[UILabel alloc] init];
    _dishCount.textColor = [UIColor blackColor];
    _dishCount.hidden = YES;
    _dishCount.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_dishCount];
    
    _addShopCart = [UIButton buttonWithType:UIButtonTypeCustom];
    [_addShopCart setBackgroundImage:[UIImage imageNamed:@"addToShopCart"] forState:UIControlStateNormal];
    [_addShopCart addTarget:self action:@selector(addToShopCart) forControlEvents:UIControlEventTouchUpInside];
    _addShopCart.layer.cornerRadius = 10.0f;
    _addShopCart.layer.masksToBounds = YES;
    [self addSubview:_addShopCart];
    
    [_logoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(5);
        make.left.equalTo(self).offset(5);
        make.bottom.equalTo(self).offset(-35);
        make.width.offset(70);
    }];
    
    [_dishName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_logoView.mas_top).offset(2);
        make.left.equalTo(_logoView.mas_right).offset(5);
        make.right.equalTo(self).offset(-5);
        make.height.offset(20);
    }];
    
    [_dishDescription mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_dishName.mas_bottom).offset(0);
        make.left.equalTo(_dishName);
        make.right.equalTo(_dishName);
        make.bottom.equalTo(_logoView.mas_bottom).offset(-8);
    }];
    
    [_monthOrders mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_dishDescription.mas_bottom).offset(0);
        make.left.equalTo(_dishDescription);
        make.right.equalTo(self).offset(-5);
        make.height.offset(20);
    }];
    
    [_price mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-2);
        make.left.equalTo(_monthOrders);
        make.right.equalTo(self).offset(-30);
        make.height.offset(20);
    }];
    
    [_addShopCart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_price.mas_top).offset(-5);
        make.right.equalTo(self).offset(-10);
        make.width.offset(25);
        make.height.offset(25);
    }];
    
    [_dishCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_addShopCart);
        make.right.equalTo(_addShopCart.mas_left).offset(-10);
        make.width.offset(25);
        make.height.offset(25);
    }];
    
    [_removeShopCart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_dishCount).offset(-4);
        make.right.equalTo(_dishCount.mas_left).offset(-10);
        make.width.offset(33);
        make.height.offset(33);
    }];
}

- (void)setDishInfo:(EWDishInfo *)dishInfo
{
    _dishInfo = dishInfo;
    if (_dishInfo.dishImage.length < 1) {
        DefaultPortraitView *defaultPortrait =
        [[DefaultPortraitView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        [defaultPortrait setColorAndLabel:self.dishInfo.businessId
                                 Nickname:self.dishInfo.dishName];
        UIImage *portrait = [defaultPortrait imageFromView];
        _logoView.image = portrait;
    }
    else{
        [_logoView sd_setImageWithURL:[NSURL URLWithString:_dishInfo.dishImage]];
    }
    
    _dishName.text = _dishInfo.dishName;
    _dishDescription.text = _dishInfo.categoryName;
    _monthOrders.text = [NSString stringWithFormat:@"月订单%@",_dishInfo.orders];
    
    _price.text = [NSString stringWithFormat:@"¥%@",_dishInfo.price];
    _price.textColor = [UIColor redColor];
    _price.font = [UIFont systemFontOfSize:13];
    _dishCount.text = [NSString stringWithFormat:@"%ld",_dishInfo.orderCount];
    if (_dishInfo.orderCount) {
        _removeShopCart.hidden = NO;
        _dishCount.hidden = NO;
    }
    else
    {
        _removeShopCart.hidden = YES;
        _dishCount.hidden = YES;
    }
}

- (void)removeFromShopCart
{
    _dishInfo.orderCount--;
    _plusBlock(_dishInfo.orderCount,NO);
    _dishCount.text = [NSString stringWithFormat:@"%ld",_dishInfo.orderCount];
    if (!_dishInfo.orderCount) {
        _removeShopCart.hidden = YES;
        _dishCount.hidden = YES;
    }
}

- (void)addToShopCart
{
    _dishInfo.orderCount++;
    _plusBlock(_dishInfo.orderCount,YES);
    _dishCount.text = [NSString stringWithFormat:@"%ld",_dishInfo.orderCount];
    if (_dishInfo.orderCount) {
        _removeShopCart.hidden = NO;
        _dishCount.hidden = NO;
    }
}

@end
